package com.xp.basic.heima.nio.netty.simpledemo.worker;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.http.*;

import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;

public class HttpDemo {
    public static void main(String[] args) {
        NioEventLoopGroup booss = new NioEventLoopGroup();
        NioEventLoopGroup worker = new NioEventLoopGroup(2);
        new ServerBootstrap()
                .group(booss,worker)
                .channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<NioSocketChannel>() {
                    @Override
                    protected void initChannel(NioSocketChannel ch) throws Exception {
                        // http handle 继承 CombinedChannelDuplexHandler 代表得是双端 handle 出站和入站都会走这里过
                        ch.pipeline().addLast(new HttpServerCodec());
                        ch.pipeline().addLast(new ChannelInboundHandlerAdapter(){
                            @Override
                            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                                if (msg instanceof HttpRequest) {
                                    System.out.println("请求头");
                                }else if (msg instanceof HttpContent){
                                    System.out.println("请求体");
                                }
                                super.channelRead(ctx,msg);
                            }
                        });

                        //这样就是注册感兴趣得事件
                        ch.pipeline().addLast(new SimpleChannelInboundHandler<HttpRequest>() {
                            @Override
                            protected void channelRead0(ChannelHandlerContext ctx, HttpRequest httpRequest) throws Exception {
                                System.out.println("解析对象类型："+httpRequest.method());
                                System.out.println(httpRequest.uri());
                                //构建响应对象
                                DefaultFullHttpResponse  httpResponse = new DefaultFullHttpResponse(httpRequest.protocolVersion(), HttpResponseStatus.OK);
                                byte[] content = "<h1>hello, tz! </h1>".getBytes(StandardCharsets.UTF_8);
                                //告诉浏览器 响应长度，不然浏览器会一直等待
                                httpResponse.headers().setInt(HttpHeaderNames.CONTENT_LENGTH,content.length);
                                httpResponse.content().writeBytes(content);

                                //写回响应
                                ctx.writeAndFlush(httpResponse);

                            }

                        });
                    }
                })
                .bind(new InetSocketAddress(8080));
    }
}
